/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.kth.lwr.jeat.samples.onsitetreatment.producers;

import javax.measure.unit.SI;
import javax.measure.unit.Unit;
import org.jscience.physics.amount.Amount;
import se.kth.lwr.jeat.core.*;
import se.kth.lwr.jeat.utils.DefaultAmountConverter;
import se.kth.lwr.jeat.utils.ProductionSupport;
import se.kth.lwr.jeat.samples.onsitetreatment.SimpleProduction;




/**
 *
 * @author davidev
 */
public class PVCPipeProduction extends SimpleProduction {
    /**
     * Produces a PVC pipe of desired type, length and lifetime. The resulting
     * impact is on year basis. There are 3 different type of pipes for use:
     *
     * <code>PVCPipeProduction.TYPE</code> <ul> <li> building110= 110 mm pipe
     * for use within buildings</li> <li> ground110= 110 mm pipe for use in
     * ground</li> <li> ground160= 160 mm pipe for use in ground</li>
     *
     * </ul>
     *
     * @param name name of this activity
     * @param description description of this activity
     * @param length lenght of pipe to produce
     * @param lifetime estimated lifetime of the pipe
     * @param pipetype type of pipe given by static TYPE variable
     */
    
   
    
    
    
    private PVCPipeProduction(ProductionSupport support) {
        super(support);
        
        
    }


    public static PVCPipeProduction get110mmBuildingInstance() {
        DefaultAmountConverter manager=new DefaultAmountConverter(SI.KILOGRAM);
        manager.setAcceptFunction(SI.METER, new AmountFunction().multiply(pipeWeight110mmIndoor));
        ProductionSupport support=new ProductionSupport("PVC pipe 110 mm indoor", PVCPipeEmissions,manager);
        return new PVCPipeProduction(support);
    }

    public static PVCPipeProduction get110mmGroundInstance() {
        DefaultAmountConverter manager=new DefaultAmountConverter(SI.KILOGRAM);
        manager.setAcceptFunction(SI.METER, new AmountFunction().multiply(pipeWeight110mmOutdoor));
        ProductionSupport support=new ProductionSupport("PVC pipe 110 mm outdoor", PVCPipeEmissions, manager);
        return new PVCPipeProduction(support);
    }
   

    public static PVCPipeProduction get160mmGroundInstance() {
        DefaultAmountConverter manager=new DefaultAmountConverter(SI.KILOGRAM);
        manager.setAcceptFunction(SI.METER, new AmountFunction().multiply(pipeWeight160mmOutdoor));
        ProductionSupport support=new ProductionSupport("PVC pipe 160 mm outdoor", PVCPipeEmissions, manager);
        return new PVCPipeProduction(support);
    }

    
    /**
     * Weight per meter of 110mm indoor PVC pipe. Tillman m.fl 1996.
     * <p/>
     * Value: 1.8
     * <p/>
     * Unit: kg/m
     */
    public static final AmountDataSource pipeWeight110mmIndoor = new AmountDataSource(
            "pipeWeight110mmIndoor",
            "Weight per meter of 110mm indoor PVC pipe. Tillman m.fl 1996.",
            Amount.valueOf(1.8, Unit.valueOf("kg/m")));
    /**
     * Weight per meter of 110 mm outdoor PVC pipe. Tillman m.fl(1996)
     * <p/>
     * Value: 1.7
     * <p/>
     * Unit: kg/m
     */
    public static final AmountDataSource pipeWeight110mmOutdoor = new AmountDataSource(
            "pipeWeight110mmOutdoor",
            "Weight per meter of 110 mm outdoor PVC pipe. Tillman m.fl(1996)",
            Amount.valueOf(1.7, Unit.valueOf("kg/m")));
    /**
     * Weight per meter of 160 mm outdoor PVC pipe. Tillman m.fl(1996)
     * <p/>
     * Value: 3.5
     * <p/>
     * Unit: kg/m
     */
    public static final AmountDataSource pipeWeight160mmOutdoor = new AmountDataSource(
            "pipeWeight160mmOutdoor",
            "Weight per meter of 160 mm outdoor PVC pipe. Tillman m.fl(1996)",
            Amount.valueOf(3.5, Unit.valueOf("kg/m")));
    /**
     * Consumtions during production of 1 kg of PVC pipe. Tillman et.al (1996)
     *
     */
    public static final InventoryDataSource PVCPipeEmissions = new InventoryDataSource("PVCPipeEmissions",
            "Consumtions during production of 1 kg of PVC pipe. Tillman et.al (1996)",
            new Inventoriable() {

                @Override
                public Inventory getInventory() {
                    Inventory retval = Inventory.emptyInventory();
                    retval.addEnergyUse(EnergyCategory.FOSSIL, Amount.valueOf(58.55, SI.MEGA(SI.JOULE.divide(SI.KILOGRAM))));
                    retval.addEnergyUse(EnergyCategory.ELECTRICAL, Amount.valueOf(3.42, SI.MEGA(SI.JOULE.divide(SI.KILOGRAM))));
                    return retval;
                }
            });

    private class PVCPipeAmountManager extends DefaultAmountConverter {

        private AmountDataSource meterWeight;

        public PVCPipeAmountManager(Unit acceptable, AmountDataSource meterWeight) {
            super(acceptable);
            this.meterWeight = meterWeight;
        }

        @Override
        public AmountFunction toCompatibleAmount(AmountDataSource amount) {
            if (amount.readValue().getUnit().isCompatible(SI.METER)) {
                return new AmountFunction().multiply(amount).multiply(meterWeight);
            }
            return super.toCompatibleAmount(amount);
        }
    }
}
